// pages/form/form.js
import { data } from './form.data.js';
var app = getApp();
Page({
  data: data,
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },

  pickerImg: function () {
    var that = this;
    pickerimg(that);
  },

  deleteimg:function(e){
    var that = this;

    that.data.outimg.splice(e.currentTarget.dataset.index, 1);

    that.setData({
      "outimg": that.data.outimg
    })
  },

  inputChange: function (e) {
    let index = e.currentTarget.dataset.index;
    let val = e.detail.value;
    let data = this.data.form;
    data[index].val = val;
    this.setData({
      'form': data
    });
  },


  submit: function () {
    let form = this.data.form;
    var that = this;
    for (let i in form) {
       if( form[i].val.length < 1 ){
          this.setData({
            action: true
          });
          return false
       }
    };
    if (this.data.outimg.length==0){
      this.setData({
        action: true
      });
      return false
    }
    let patientid = wx.getStorageSync('userInfo').patientid;
    let detail = {
      weight: form[0].val,
      height: form[1].val,
      waist: form[2].val,
      hip: form[3].val
    };
    detail = JSON.stringify(detail);
    var params = {
      patientid: patientid,
      type: 10,
      detail: detail,
      img: this.data.outimg[0],
      times:8 
    };
    app.https.post(that,'/IbabyWebService/camp/submitDiet',params,function(rel){
      console.log(rel)
      let userName = wx.getStorageSync('userInfo').realname;
      var look={
        patientId: patientid,
        detail:detail,
        remark:'',
        userName: userName
      }
      app.https.post(that,'/aiding-web/api/sendDate',look,function(res){
        console.log(rel.dietId)
        wx.redirectTo({
        url: '../datalist/datalist?id=' + rel.dietId,
      })
      })
    })
  }
})


//更换图片
function  pickerimg(that) {

  wx.chooseImage({
    count: 1, // 最多可以选择的图片张数，默认9
    sizeType: ['compressed'], // original 原图，compressed 压缩图，默认二者都有
    sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
    success: function (res) {
      console.log(res)

      // success
      var imgpath = res.tempFilePaths;
      console.log(imgpath)
      

      for (var i = 0;i<imgpath.length;i++){
        uploadfile(that,imgpath[i]);
      }


    }
  })
}


//图片上传(可以多张照片展示)
function uploadfile(that,imgpath){

  app.https.uploadfile(that, '/IbabyWebService/File/Upload', imgpath, function (res) {
    var imgpath = app.https.baseurl + res;
    console.log(imgpath)
    that.setData({
      "setimg":[]
    })

    that.data.setimg.push(imgpath);

    that.setData({
      "setimg": that.data.setimg,
      'outimg': that.data. setimg,
    
    });

    console.log(that.data.outimg)
  })
}